package com.sc.nft.admin.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.GroupBannedRequest;
import com.sc.nft.annotation.DistributedLock;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dto.IdAffirmDTO;
import com.sc.nft.entity.dto.AdminModifierGroupDTO;
import com.sc.nft.entity.dto.ImCreateGroupDTO;
import com.sc.nft.entity.vo.AdminGroupListVO;
import com.sc.nft.service.ImGroupService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("admin/im")
@Api(tags = "即时通讯")
public class ImController {

    private final ImGroupService imGroupService;

    @ApiOperation(value = "创建群组", notes = "创建群组", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/adminCreateGroup", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result adminCreateGroup(@RequestBody ImCreateGroupDTO imCreateGroupDTO) {
        imCreateGroupDTO.setApplyJoinOption("FreeAccess");
        imGroupService.adminCreateGroup(imCreateGroupDTO);
        return Result.success();
    }

    @ApiOperation(value = "解散群", notes = "解散群", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/adminDestroyGroup", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @DistributedLock(value = {"modifierGroup","#{adminModifierGroupDTO.groupId}"},message = "已解散,禁止重复提交")
    public Result adminDestroyGroup(@Valid @RequestBody IdAffirmDTO idAffirmDTO) {
        imGroupService.adminDestroyGroup(idAffirmDTO.getGroupId());
        return Result.success();
    }

    @ApiOperation(value = "群列表", notes = "群列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/adminGroupList", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Page<AdminGroupListVO>> adminGroupList(@RequestBody PageRequest pageRequest) {
        Page<AdminGroupListVO> adminGroupListVOPage = imGroupService.adminGroupList(pageRequest);
        return Result.success(adminGroupListVOPage);
    }

    @ApiOperation(value = "修改群", notes = "修改群", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/modifierGroup", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result modifierGroup(@RequestBody AdminModifierGroupDTO adminModifierGroupDTO) {
        imGroupService.modifierGroup(adminModifierGroupDTO);
        return Result.success();
    }


    @ApiOperation(value = "所有用户加入群组", notes = "所有用户加入群组", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/allUserAddGroup", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result allUserAddGroup() {
        imGroupService.voluntarilyAllUserAddGroup();
        return Result.success();
    }


    @ApiOperation(value = "封禁群组or解封群组", notes = "封禁群组or解封群组", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/groupBanned", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result groupBanned(@RequestBody GroupBannedRequest groupBannedRequest) {
        imGroupService.groupBanned(groupBannedRequest.getGroupId(),groupBannedRequest.getOperation());
        return Result.success();
    }
}
